--- 模块功能：新设备发送HTTP请求获取clientNo.
-- @author zhangfenghe
-- @module Tweeter.getNewClientNo
-- @license MIT
-- @copyright openLuat
-- @release 2022.04.26
module(..., package.seeall)

require "http"
require "config"
require "nvm"
require "getLbsLoc"
require "mqttTask"
require "mqttTaskKeson"
require "pressureRealTime"
require "mqttTask61613"
-- 初始化nvm获取配置参数
nvm.init("config.lua", true)

local clientNo, pKey, deviceTypeId, restartOrNot, sn, meId, sysId,imei,iccid = nvm.get("ClientNo"), nvm.get("PKey"),
    nvm.get("DeviceTypeId"), false, nvm.get("Sn"), nvm.get("MeId"), nvm.get("SysId"),nvm.get("IMEI"),nvm.get("ICCID")
local function mqttTaskStart()
    mqttTask.startMQTTClient(sn)
end
local function mqttTaskKesonStart()
    mqttTaskKeson.startMQTTClientKeson(sn)
end
local function mqttTask61613Start()
    mqttTask61613.startMQTTClient(sn)
end
--[[ local function getPressureDataLocal()
    pressureRealTime.timedAcquisitionOfPressure()
end ]]
local function cbFnc(result, prompt, head, body)
    log.info("getNewClientNo.cbFnc", result, prompt)
    if result and head then
        for k, v in pairs(head) do
            log.info("getNewClientNo.cbFnc", k .. ": " .. v)
        end
    else
        log.error("获取新设备号请求头错误！")
    end
    if result and body then
        log.info("getNewClientNo.cbFnc", "bodyLen=" .. body:len())
        log.info("body", body)
        clientNo = body
        for i = 1, 10 do
            log.info("[clientNo:]" .. clientNo .. "\n")
        end
        imei = misc.getImei()
        iccid = sim.getIccid()
        sn = pKey .. deviceTypeId .. clientNo
        meId = sysId .. sn
        nvm.set("Longitude",getLbsLoc.Lng)
        nvm.set("Latitude",getLbsLoc.Lat)
        nvm.set("ClientNo", clientNo)
        nvm.set("IMEI", imei)
        nvm.set("ICCID", iccid)
        nvm.set("Sn", sn)
        nvm.set("MeId", meId)
        sys.restart("参数存储，设备重启")
    end
    log.info("getAndSetClientNo")
end

-- 获取新设备号
local function get_new_client_no()
    while misc.getImei() == "" and string.len(misc.getImei()) == 0 do
        log.info("wait imei get")
        sys.wait(1000)
    end
    local tjsondata, result, errinfo = json.decode(clientNoJson)
    log.info("result jsonSecode", result)
    if result then
        log.info("imei:", misc.getImei())
        log.info("imei:", tjsondata[misc.getImei()])
        nvm.set("ClientNo", tjsondata[misc.getImei()], nil, true)
        sys.restart("参数存储，设备重启！")
    end
end

-- 获取并设置设备号
local function getAndSetClientNo()
    log.info("SetClientNo", clientNo)
    if clientNo == "5263000000" or clientNo == nil then
        http.request("GET", "http://47.104.133.93:8090/api/genDeviceNo/5263", nil, nil, nil, nil, cbFnc)
    else
        if restartOrNot then
            sys.restart("参数存储，设备重启！")
        end
        --nvm.restore()
        --sys.restart("参数存储，设备重启")
        --sn = pKey .. deviceTypeId .. clientNo
        --meId = sysId .. sn
        -- nvm.set("Sn", sn)
        --nvm.set("MeId", meId)
        --sys.restart("参数存储，设备重启")
        sys.taskInit(mqttTaskStart)
        sys.taskInit(mqttTaskKesonStart)
        sys.taskInit(mqttTask61613Start)
        -- sys.taskInit(getPressureDataLocal)
    end
end
getAndSetClientNo()
